package com.hotelvip.common.utils;

import com.hotelvip.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * @author Lzh
 * @date 2020/11/30 15:42
 */
public class ShiroUtils {
    /**
     * 获取当前用户用户名
     * @return
     */
    public static String getUsername() {
        return getUser().getUsername();
    }

    /**
     * 获取当前用户
     * @return
     */
    public static SysUser getUser() {
        Subject subject = getSubject();
        if(subject == null || subject.getPrincipal() == null) {
            return new SysUser();
        }

        return (SysUser) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 获取id
     * @return
     */
    public static int getUserId() {
        return getUser().getId();
    }

    /**
     * 获取Subject
     * @return
     */
    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }


}
